#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include<sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>
using namespace std;

//主线程id, 线程的局部存储.不同于全局变量是给全部线程都能使用!
// 线程里面gval一直在改变, 但是主线程始终没有变.
__thread pid_t lwp = 0;

__thread int gval = 100;

pid_t gettid() 
{
    return syscall(SYS_gettid);
}

void* ThreadRountine(void* args)
{
    const string name = static_cast<const char*>(args);

    lwp = gettid();

    while(true)
    {
        sleep(1);
        cout << name << "gavl:" << gval << "  ,&gval:" << &gval << endl;
        cout << "new thread" << lwp << endl;
        gval++;
    }

    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, ThreadRountine, (void*)"thread-1");
    lwp = gettid();
    cout << "man thread" << lwp << endl;

    while(true)
    {
        sleep(1);
        cout << "man thread" << gval << "  , &gval:" << &gval << endl;
    }

    pthread_join(tid, nullptr);
    return 0;
}